package com.chijun.mform.show;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chijun.mform.R;
import com.chijun.mform.full.mode.ModeFullForm;

import java.util.List;
import java.util.Map;

/**
 * 提交表单简要信息
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2025/1/7 23:09
 */
public class ApproximateInformationOnTheSubmittedFormDataAdapter extends RecyclerView.Adapter<ApproximateInformationOnTheSubmittedFormDataAdapter.ViewHolder>{
    List<ModeShowFullForm> fullFormList;
    Map<Long,Map<String,String>> showFieldMap;

    /**
     * 字段类型数组
     *
     * @param fullFormList 表单列表
     * @param showFieldMap 0
     */
    public ApproximateInformationOnTheSubmittedFormDataAdapter(List<ModeShowFullForm> fullFormList,Map<Long,Map<String,String>> showFieldMap){
        this.fullFormList = fullFormList;
        this.showFieldMap = showFieldMap;

    }

    @NonNull
    @Override
    public ApproximateInformationOnTheSubmittedFormDataAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ApproximateInformationOnTheSubmittedFormDataAdapter.ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_show_full_data, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ApproximateInformationOnTheSubmittedFormDataAdapter.ViewHolder holder, int position) {
        ModeShowFullForm modeFullForm = fullFormList.get(position);

        //表单名称处理
        String fullName = "";
        if (modeFullForm.getFormName() != null) fullName = modeFullForm.getFormName();
        if (modeFullForm.getSponsorName() != null) fullName += modeFullForm.getSponsorName();
        holder.fullName.setText(fullName);

        if (showFieldMap != null) {
            Map<String, String> showField = showFieldMap.get(modeFullForm.getId());
            //显示字段处理
            if (showField == null || showField.isEmpty()) {
                holder.allFieldLayout.setVisibility(View.GONE);
            } else {
                holder.allFieldLayout.setVisibility(View.VISIBLE);
                if (showField.size() == 1) {
                    holder.twoFieldLayout.setVisibility(View.GONE);
                } else {
                    holder.twoFieldLayout.setVisibility(View.VISIBLE);
                }
                int i = 0;
                for (String fieldName : showField.keySet()) {
                    String fieldContent = showField.get(fieldName);
                    if (i == 0) {
                        holder.oneName.setText(fieldName);
                        holder.oneContent.setText(fieldContent);
                    } else if (i == 1) {
                        holder.twoName.setText(fieldName);
                        holder.twoCOntent.setText(fieldContent);
                    } else {
                        break;
                    }
                    i++;
                }
            }
        } else {
            holder.allFieldLayout.setVisibility(View.GONE);
        }

        //表单填写日期
        if (modeFullForm.getInitiationTime() != null) {
            holder.time.setVisibility(View.VISIBLE);
            holder.time.setText(modeFullForm.getInitiationTime());
        } else {
            holder.time.setVisibility(View.INVISIBLE);
        }

        if (modeFullForm.getState()==0) {
            holder.state.setText("正在审批");
        } else if (modeFullForm.getState()==1) {
            holder.state.setText("审批通过");
        } else if (modeFullForm.getState()==2) {
            holder.state.setText("审批拒绝");
        }
    }

    @Override
    public int getItemCount() {
        return fullFormList == null ? 0 : fullFormList.size();
    }

    public static class ViewHolder extends  RecyclerView.ViewHolder{
        private final TextView fullName,oneName,oneContent,twoName,twoCOntent,time,state;
        private final LinearLayout oneFieldLayout,twoFieldLayout,allFieldLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            oneFieldLayout = itemView.findViewById(R.id.show_full_data_field_one_layout);
            twoFieldLayout = itemView.findViewById(R.id.show_full_data_field_two_layout);
            allFieldLayout = itemView.findViewById(R.id.show_full_data_field_all_layout);
            fullName = itemView.findViewById(R.id.adapter_show_full_data_name);
            oneName = itemView.findViewById(R.id.show_full_data_field_one_layout_name);
            oneContent = itemView.findViewById(R.id.show_full_data_field_one_layout_content);
            twoName = itemView.findViewById(R.id.show_full_data_field_two_layout_name);
            twoCOntent = itemView.findViewById(R.id.show_full_data_field_two_layout_content);
            time = itemView.findViewById(R.id.show_full_data_submit_time);
            state = itemView.findViewById(R.id.show_full_data_state);

        }
    }
}
